package com.ruoyi.project.learn.sscl.controller;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.learn.sscl.domain.DjRealTimeMaterial;
import com.ruoyi.project.learn.sscl.service.IDjRealTimeMaterialService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 实时材料Controller
 * 
 * @author ruoyi
 * @date 2025-09-17
 */
@Controller
@RequestMapping("/dj/realtime/material")
public class DjRealTimeFilesController extends BaseController {
    /**
     * 查询实时材料列表
     */
    @PostMapping("/list")
    @ResponseBody
    public Object list(String title, String contentType) {
        String filePath = RuoYiConfig.getUploadPath();
        String pdfFilePath = filePath + "/static";
        
        // 创建一个 List 来存放所有文档 Map
        List<Map<String, Object>> demoDocuments = new ArrayList<>();
        
        // 读取目录下的所有文件
        File dir = new File(pdfFilePath);
        if (dir.exists() && dir.isDirectory()) {
            File[] files = dir.listFiles();
            if (files != null) {
                for (int i = 0; i < files.length; i++) {
                    File file = files[i];
                    if (file.isFile()) {
                        Map<String, Object> doc = new HashMap<>();
                        doc.put("id", i + 1);
                        String t = file.getName();
                        if(StringUtils.isNotBlank(title) && !t.contains(title)) {
                            continue;
                        }
                        doc.put("title", t);
                        doc.put("source", "系统上传");
                        String ct = getFileExtension(file.getName());
                        if(StringUtils.isNotBlank(contentType) && !ct.contains(contentType)) {
                            continue;
                        }
                        doc.put("contentType", ct);
                        doc.put("contentUrl", "/profile/upload/static/" + file.getName());
                        doc.put("readCount", 0);
                        doc.put("status", 1);
                        doc.put("publishTime", LocalDate.now());
                        doc.put("createTime", LocalDate.now());
                        demoDocuments.add(doc);
                    }
                }
            }
        }

        return demoDocuments;
    }
    
    /**
     * 获取文件扩展名
     */
    private String getFileExtension(String fileName) {
        if (fileName == null || fileName.lastIndexOf(".") == -1) {
            return "unknown";
        }
        return fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
    }
}
